package com.wzq.solution;

import com.wzq.solution.node.ListNode;

class Solution19 {
    public static ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode fast =head;
        ListNode slow =head;
        while(n-- > 0){
            fast = fast.next;
        }

        if(fast == null){
            if(head.next == null){
                return null;
            }else {
                head.val = head.next.val;
                head.next = head.next.next;
                return head;
            }
        }


        while(fast.next != null){
            fast = fast.next;
            slow = slow.next;
        }

        slow.next = slow.next.next;
        return head;
    }

    public static void main(String[] args) {
        int[] headArr = new int[]{1};
        ListNode head = new ListNode(headArr[0]);
        ListNode p =head;
        for (int i=1;i<headArr.length; i++){
            p.next = new ListNode(headArr[i]);
            p = p.next;
        }

         head = removeNthFromEnd(head,1);
        while (head != null){
            System.out.println(head.val);
            head = head.next;
        }
        //System.out.println(964632435*10+1>Math.pow(2,31)-1);
    }
}